/*-----------------------------------------------------------------------
|                            FILE DESCRIPTION                           |
-----------------------------------------------------------------------*/
/*----------------------------------------------------------------------
  - File name     : strutil.h
  - Author        : liuzhihua (liuzhihuawy@163.com)
  - Update date   : 2023.05.09                  
  -	File Function : string utils
  - GB2312 Encoding
-----------------------------------------------------------------------*/
/*-----------------------------------------------------------------------
|                               UPDATE NOTE                             |
-----------------------------------------------------------------------*/
/**
  * Update note:
  * ------------   ---------------   ----------------------------------
  *     Date            Author                       Note
  * ------------   ---------------   ----------------------------------         
  *   2023.5.1        liuzhihua                (+) Create File 
***/

#ifndef _STRUTIL_HPP_
#define _STRUTIL_HPP_


/*-----------------------------------------------------------------------
|                               INCLUDES                                |
-----------------------------------------------------------------------*/
#include "stdint.h" /* uint8_t ... */
#include "string.h" /* memcmp memcpy */
#include "math.h"   /* fabs */
#include "stdio.h"  /* memcpy printf */
#include "stdlib.h" /* atol atof */

/*-----------------------------------------------------------------------
|                                DEFINES                                |
-----------------------------------------------------------------------*/
class StrUtil
{
    private:
        uint8_t split(uint8_t *str, uint8_t size);  //分割器
    
    public:
        uint8_t *phead;     // 帧头
        uint8_t headSize;   // 帧头长度
        uint8_t separator;  // 分隔符
        uint8_t *ptail;     // 帧尾
        uint8_t tailSize;   // 帧尾长度
        uint8_t *pBuf;      // 数据缓存区
        uint8_t BufSize;    // 缓冲区大小
        uint8_t BufUsage;   // 分隔符出现的数量/缓存的使用量
        uint8_t *pSrcAdr;   // 检测地址
        uint8_t SrcAdrSize; // 检测缓存的大小
        int headPos;        // 帧头位置
        int tailPos;        // 帧尾位置

        StrUtil(const char *head,
                char sep,
                const char *tail,
                uint8_t *pIndexBuf,
                uint8_t bufSize,
                uint8_t *SrcAddress,
                uint8_t SrcSize);

        int parse(uint8_t* data, uint16_t size);//解析
        int parse(char *str);
        int parse(void);
        
        void setSrcAddress(uint8_t* SrcAddress, uint8_t Size);//设置解析地址

        int checkHead(uint8_t* data, uint16_t size);//检查帧头
        int checkHead(char *str);
        int checkHead(void);

        int checkTail(uint8_t* data, uint16_t size);//检查帧尾
        int checkTail(char *str);
        int checkTail(void);

        bool checkSplitCondition(uint8_t* data, uint16_t size);//检查帧条件
        bool checkSplitCondition(char *str);
        bool checkSplitCondition(void);

        uint16_t getLen(uint8_t index);
        int getInt(uint8_t index);
        char* getStr(uint8_t index);
        double getDouble(uint8_t index);
        uint8_t* getData(uint8_t index);
        bool strCmp(uint8_t index, char *strNeedCmpare);
        bool strCmp(uint8_t index, const char *strNeedCmpare);
};

/*-----------------------------------------------------------------------
|                             API FUNCTION                              |
-----------------------------------------------------------------------*/

extern "C" 
{
	int StrUtilMemSearch(uint8_t* str, uint8_t Size, uint8_t* phead, uint8_t headSize);
}


#endif
